package mainController;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;

import controller.ClientAppController;
import controller.ClientLoginController;
import controller.MessageReceiver;

import mainView.ClientView;
import model.AnswerObject;

public class ClientController {
	
	private Socket socket;
	private ObjectOutputStream bufferOut;
	private ObjectInputStream bufferIn;
	private MessageReceiver messageReceiver;
	
	private ClientLoginController clientLoginController;
	private ClientAppController clientAppController;
	
	private ClientView clientView;
	
	public ClientController() {
		this.clientLoginController = new ClientLoginController(this);
		this.clientAppController = new ClientAppController(this);
		this.clientView = new ClientView();

		this.messageReceiver = new MessageReceiver(this);
		this.configureClientView();
		
		this.configureViewToLogin();
	}
	
	private void configureClientView() {		
		this.clientView.setLocationRelativeTo(null);
		this.clientView.setVisible(true);
		this.clientView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}	
	
	private void configureViewToLogin() {		
		this.getClientView().getjPanel().setVisible(false);
		this.getClientView().getjPanel().removeAll();
		this.getClientView().getjPanel().add(this.clientLoginController.getPanel());
		this.getClientView().getjPanel().setVisible(true);		
	}
	
	private void configureViewToApp() {		
		this.getClientView().getjPanel().setVisible(false);
		this.getClientView().getjPanel().removeAll();
		this.getClientView().getjPanel().add(this.clientAppController.getPanel());
		this.getClientView().getjPanel().setVisible(true);		
	}	
	
	private ClientView getClientView() {
		return this.clientView;
	}

	public void createConnection(String host, int port) throws UnknownHostException, IOException {
	    this.socket = new Socket(host, port);
	    this.bufferOut = new ObjectOutputStream(this.socket.getOutputStream());
        this.bufferIn = new ObjectInputStream(this.socket.getInputStream());
	}
	
	public void closeConnection() {
        try {        	
        	this.bufferOut.close();
			this.bufferIn.close();
			this.socket.close();
		}
        catch (IOException e) {
        	System.err.println("ClientController:: " + e);
        	System.exit(1);
		}
        System.exit(0);
	}	

	public void sendMessage(String licensePlate) {
		try {
			this.bufferOut.writeObject(licensePlate);
		}
		catch (IOException e) {
			System.err.println("ClientController:: Couldn't write message: " + licensePlate);
		}
	}	
	
	public void receiveMessage(AnswerObject message) {
		this.clientAppController.show(message);
	}

	public Socket getSocket() {
		return socket;
	}

	public ObjectOutputStream getBufferOut() {
		return bufferOut;
	}

	public ObjectInputStream getBufferIn() {
		return bufferIn;
	}

	public MessageReceiver getMessageReceiver() {
		return messageReceiver;
	}			
	
	public void handshake(String code) throws IOException, ClassNotFoundException {
		this.createConnection("localhost", 8000);		
		
		this.bufferOut.writeObject(code);
		while (true) {
			String message = (String) this.bufferIn.readObject();
			if (message == null) continue;
			
			if (message.equalsIgnoreCase("ok")) {
				this.configureViewToApp();
				this.messageReceiver.start();				
				return;
			}
			else if (message.equalsIgnoreCase("wrong_code")) {
				throw new IOException();				
			}			
			else {
				System.err.println("Access Denied");
				System.exit(1);
			}
		}
	}

	public static void main(String args[]) {
		new ClientController();	
	}	
}
